<template>
  <div class="ranking_container">
    <div class="ranking_header">
      <span class="ranking_title_main">
        <svg-icon icon-class="line_chart_1"></svg-icon>实时热点
      </span>
      <span class="ranking_source">{{ sourceType }}</span>
      <el-button type="text" class="refresh_btn" @click="handleRefresh">
        <i class="el-icon-refresh"></i>
        换一换
      </el-button>
    </div>
    <div class="ranking_list">
      <div v-for="(item, index) in list" :key="item.id" class="ranking_item" :class="{ 'active': activeIndex === index }" @click="handleClick(index)">
        <span class="ranking_index">{{ index + 1 }}</span>
        <el-tooltip :content="item.title" placement="top" :disabled="!isTitleOverflow">
          <span class="ranking_title" ref="titleRefs" @mouseenter="checkTitleOverflow(index)">
            {{ item.title }}
          </span>
        </el-tooltip>
        <span class="ranking_hot">{{ item.hot }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    list: Array,
    sourceType: String
  },
  data() {
    return {
      activeIndex: -1,
      isTitleOverflow: false,
      refreshInterval: null
    }
  },
  mounted() {
    this.startAutoRefresh()
  },
  beforeDestroy() {
    this.stopAutoRefresh()
  },
  methods: {
    handleRefresh() {
      this.$emit('refresh')
    },
    handleClick(index) {
      this.activeIndex = index
      this.$emit('select', this.list[index])
    },
    checkTitleOverflow(index) {
      const titleEl = this.$refs.titleRefs[index]
      this.isTitleOverflow = titleEl.scrollWidth > titleEl.clientWidth
    },
    startAutoRefresh() {
      this.refreshInterval = setInterval(() => {
        this.handleRefresh()
      }, 10000)
    },
    stopAutoRefresh() {
      if (this.refreshInterval) {
        clearInterval(this.refreshInterval)
        this.refreshInterval = null
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/mixin.scss';

.ranking_container {
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  width: 100%;
  height: 100%;
  overflow: auto;
}

.ranking_header {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin-bottom: 24px;
  padding: 8px 12px;
  background: #f5f7fa;
  border-radius: 8px;
  width: 100%;
  gap: 16px;
  font-size: 20px;

  .ranking_title_main {
    color: #ff4a0e;
    font-weight: 500;
    gap: 8px;

    .svg-icon {
      margin-right: 6px;
    }
  }

  .ranking_source {
    font-size: 14px;
    font-weight: 500;
    font-size: 14px;
    padding: 4px 8px;
    border-radius: 4px;
    color: #737373;
  }
}

.refresh_btn {
  padding: 6px 12px;
  color: #606266;
  font-size: 14px;
  border-radius: 6px;
  transition: all 0.2s ease;

  i {
    margin-right: 4px;
  }
}

.refresh_btn:hover {
  color: #409eff;
  background: rgba(64, 158, 255, 0.1);
}

.ranking_list {
  display: flex;
  gap: 12px;
}

.ranking_item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.ranking_item:hover {
  background-color: #f5f7fa;
}

.ranking_item.active {
  background-color: #ecf5ff;
}

.ranking_index {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: #909399;
}

.ranking_item:nth-child(1) .ranking_index {
  color: #f56c6c;
}

.ranking_item:nth-child(2) .ranking_index {
  color: #e6a23c;
}

.ranking_item:nth-child(3) .ranking_index {
  color: #67c23a;
}

.ranking_title {
  flex: 1;
  font-size: 14px;
  color: #606266;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

.ranking_hot {
  font-size: 12px;
  color: #f56c6c;
}
</style>
